package com.cn.ccp.action.system;

import java.io.File;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.UserBean;

public class RecoveryUserAction extends BaseAction {

	private Logger log = Logger.getLogger(RecoveryUserAction.class);
	private List<UserBean> userList ;
	
	public RecoveryUserAction(){
		log.info("-----------RecoveryUserAction()------------------------");
		getpagedetail();
	}
	
	 private boolean selectAll; 
		public String disAndselectAll() {  
	        for (UserBean user : userList) {  
	        	if(user.getId() != 1)
	        		user.setDisable(selectAll);  
	        } 
	        return "";
	    }  

		public boolean isSelectAll() {
			return selectAll;
		}

		public void setSelectAll(boolean selectAll) {
			this.selectAll = selectAll;
		}

	
	
	
	public void recoveryUsers(){
		log.info("-----------recoveryUsers()------------------------");
		for(UserBean u : userList){
			if(u.isDisable())
				getTeacherService().recovery(u.getId());
		}
		getpagedetail();
	}
	
	public void deleteUsers(){
		log.info("-----------deleteUsers()------------------------");
		for(UserBean u : userList){
			if(u.isDisable()){
				if(u.getUserType().equals("1") ){
					getStudentService().deleteStudent(u.getId());
				}else
					getTeacherService().deleteTeacher(u.getId());
				File sourseFile = null;
				if(u.getFile_path_id() != -1){//删除用户图片和文件数据
					sourseFile = new File(getFilePathService().findFilePathById(u.getFile_path_id()).getPath() + u.getId());
					deleteFile(sourseFile);
				}
			}
		}
		selectAll = false;
		getpagedetail();
	}
	
	
	
	public void delete(){
		try {
			FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
			String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
			log.info("-------------delete()---------id = "+id);
			if(id == null || id.equals("null") || id.equals(""))
				return ;
			if(id.equals("1") ){
				return ;
			}
			String userType = (String)context.getExternalContext().getRequestParameterMap().get("userType");
			if(userType.equals("1") ){
				getStudentService().deleteStudent(Integer.parseInt(id));
			}else{
				getTeacherService().deleteTeacher(Integer.parseInt(id));
			}
			String file_path_id = (String)context.getExternalContext().getRequestParameterMap().get("file_path_id");
			File sourseFile = null;
			if(!file_path_id.equalsIgnoreCase("-1")){//删除用户图片和文件数据
				sourseFile = new File(getFilePathService().findFilePathById(Integer.parseInt(file_path_id)).getPath() + id);
				deleteFile(sourseFile);
			}
			getpagedetail();
		} catch (Exception e) {
		}
	}
	
	private void deleteFile(File file){ 
		   if(file.exists()){ 
		    if(file.isFile()){ 
		     file.delete(); 
		    }else if(file.isDirectory()){ 
		     File files[] = file.listFiles(); 
		     for(int i=0;i<files.length;i++){ 
		      this.deleteFile(files[i]); 
		     } 
		    } 
		    file.delete(); 
		   }else{ 
		    log.info("所删除的文件不存在！"); 
		   } 
		} 
	
	
	
	/**
	 * 恢复用户登录状态
	 */
	public void recovery(){
		try {
			FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
			String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
			log.info("-------------recovery()---------id = "+id);
			if(id == null || id.equals("null") || id.equals(""))
				return ;
			if(id.equals("1") ){
				return ;
			}
			getTeacherService().recovery(Integer.parseInt(id));
			getpagedetail();
		} catch (Exception e) {
			log.error("recover user failed ; ", e);
		}
	}
	
	@Override
	public void getpagedetail() {
		setItemCount(getTeacherService().getDisableCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		if(start < 0)
			start = 0 ;
		userList = getTeacherService().queryDisableUsers(start, pagesize);
	}
	
	
	public List<UserBean> getUserList() {
		return userList;
	}
	public void setUserList(List<UserBean> userList) {
		this.userList = userList;
	}
	
	
}
